package com.sendables;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.awt.image.FilteredImageSource;
import java.awt.image.ImageFilter;
import java.awt.image.ImageProducer;
import java.awt.image.RGBImageFilter;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import util.Util;

import com.exception.IllegalPackageException;

public class Cursor extends Paintable {
	private static Image	cursor;
	private static Color	cursorColor;

	public Cursor(int ownerId, Color c, DoodlePoint start)
	{
		super(ownerId, c, start);
	}

	public Cursor()
	{
		super();
	}

	public Image createMyCursor()
	{
		BufferedImage bi;
		cursorColor = c;
		try
		{
			File f = new File(System.getProperty("user.dir"));
			bi = ImageIO.read(new File(f.getParent() + Util.getSystemPath("\\imgs\\cursor.png")));
			ImageFilter filter = new RGBImageFilter() {
				public int	makeTransparent		= Color.BLACK.getRGB() | 0xFF000000;
				public int	makeThisColor		= 0xFFFFFFFF;
				public int	makeInvOfThisColor	= 0xFF7F7F7F;

				public final int filterRGB(int x, int y, int rgb)
				{
					if ((rgb | 0xFF000000) == makeTransparent)
					{
						/* we should make it transparent! */
						return 0x00FFFFFF & rgb;
					} else if (rgb == makeThisColor)
					{
						/* regular... just use this take this color */
						return 0xFF000000 | c.getRGB();
					} else if (rgb == makeInvOfThisColor)
					{
						/* inverted.. take this color and inverted... */
						return 0xFF000000 | ~c.getRGB();
					} else
					{
						/* if this happend something is wrong with the coursor.png file.. */
						System.out.println("Invalid cursor.png file... Exiting...");
						System.exit(1);
						return rgb;
					}
				}
			};

			ImageProducer ip = new FilteredImageSource(bi.getSource(), filter);
			return Toolkit.getDefaultToolkit().createImage(ip);
		} catch (IOException e)
		{
			e.printStackTrace();
		}
		return null;
	}

	public void paint(Graphics g)
	{
		if (cursor == null || !cursorColor.equals(c))
		{
			cursor = createMyCursor();
		}
		g.drawImage(cursor, start.getX(), start.getY(), null);
	}

	@Override
	public void writeByteRepresentation(byte[] b, int[] offset)
	{
		b[offset[0]++] = Sendable.CURSOR;
		super.writeByteRepresentation(b, offset);
	}

	public String toString()
	{
		return getClass().getCanonicalName() + super.toString();
	}

	public Sendable parse(byte[] b, int[] offset) throws IllegalPackageException
	{
		if (b[offset[0]++] != Sendable.CURSOR)
			throw new IllegalPackageException("Trying to parse something that isnt a line in class: Line");
		parseSuper(b, offset);
		return this;
	}

	public static void main(String[] args) throws IllegalPackageException
	{
		Cursor c = new Cursor(3434, Color.red, new DoodlePoint(23423, 2323));
		byte[] b = new byte[c.getAlloc()];
		c.writeByteRepresentation(b, new int[] { 0 });
		Cursor cc = (Cursor) new Cursor().parse(b, new int[] { 0 });
		System.out.println(c);
		System.out.println(cc);
	}

	@Override
	public int getAlloc()
	{
		return super.getAlloc();
	}

}
